/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.modele;

import client.modele.InterfaceModele;
import Exceptions.InputAnnuiteException;
import Exceptions.InputChoixException;
import Exceptions.InputMontantException;
import Exceptions.InputNbAnException;
import Exceptions.InputTxInteretException;
import exception.CommunicationException;
import exception.CommunicationResponseApplicationException;
import exception.ReadingObjectException;

/**
 *
 * @author KB
 */
public class ModeleLocal implements InterfaceModele {

    @Override
    public double envoyerDemandeAnnuites(double montant, double txInterets, int nbAnnuite) throws CommunicationResponseApplicationException, InputAnnuiteException, InputChoixException, InputTxInteretException, InputMontantException, InputMontantException, InputNbAnException, ReadingObjectException {
        return calculAnnuite(montant, nbAnnuite, txInterets);
    }

    @Override
    public double envoyerDemandeNbAnnees(double montant, double txInterets, double annuite) throws CommunicationResponseApplicationException, InputAnnuiteException, InputChoixException, InputTxInteretException, InputMontantException, InputMontantException, InputNbAnException, ReadingObjectException {
        return calculNbAnnees(annuite, txInterets, montant);
    }

    private double calculAnnuite(double montant, int nbAnnees, double txInteret) {
        txInteret = txInteret / 100;
        double puissance = Math.pow(1 + txInteret, nbAnnees);
        return montant * txInteret * (puissance / (puissance - 1));
    }

    private double calculNbAnnees(double annuite, double txInteret, double montant) {
        txInteret = txInteret / 100;
        double numerateur = Math.log(1 - ((txInteret * montant) / annuite));
        double denominateur = Math.log(1 + txInteret);
        return -(numerateur / denominateur);
    }
}
